
package org.example.common;

import org.example.enums.CommonErrorCode;
import org.example.enums.ErrorCode;

import java.io.Serializable;
import java.util.Collection;

/**
 * @author tianshuo
 * @version Id: Result.java, v 0.1 2016/7/27 0027 17:01 tianshuo Exp $$
 */
public class Result<T> implements Serializable {

    private static final long serialVersionUID = -4767429537969916917L;

    private T data;
    private String code;
    private String message;

    public Result() {
    }

    public Result(T data, String code, String message) {
        this.data = data;
        this.code = code;
        this.message = message;
    }

    public Result(T data) {
        this.data = data;
        this.setCode(CommonErrorCode.SUCCESS_MESSAGE.getCode());
        this.setMessage(isEmpty(message) ? CommonErrorCode.SUCCESS_MESSAGE.getDesc() : message);
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void setErrorMessage(String code, String message) {
        this.setCode(isEmpty(code) ? CommonErrorCode.SYSTEM_INNER_ERROR.getCode() : code);
        this.setMessage(isEmpty(message) ? CommonErrorCode.SYSTEM_INNER_ERROR.getDesc() : message);
    }

    public void setError() {
        this.setCode(CommonErrorCode.SYSTEM_INNER_ERROR.getCode());
        this.setMessage(isEmpty(message) ? CommonErrorCode.SYSTEM_INNER_ERROR.getDesc() : message);
    }

    public void setSuccessMessage(String message) {
        this.setCode(CommonErrorCode.SUCCESS_MESSAGE.getCode());
        this.setMessage(isEmpty(message) ? CommonErrorCode.SUCCESS_MESSAGE.getDesc() : message);
    }

    public void setSuccess() {
        this.setCode(CommonErrorCode.SUCCESS_MESSAGE.getCode());
        this.setMessage(isEmpty(message) ? CommonErrorCode.SUCCESS_MESSAGE.getDesc() : message);
    }


    public static Result success() {
        Result result = new Result();
        result.setCode(CommonErrorCode.SUCCESS_MESSAGE.getCode());
        result.setMessage(CommonErrorCode.SUCCESS_MESSAGE.getDesc());
        return result;
    }

    public static <T> Result<T> success(T t) {
        Result<T> result = new Result<>();
        result.setCode(CommonErrorCode.SUCCESS_MESSAGE.getCode());
        result.setMessage(CommonErrorCode.SUCCESS_MESSAGE.getDesc());

        result.setData(t);
        return result;
    }

    public static <T> Result<T> error() {
        return error(CommonErrorCode.SYSTEM_INNER_ERROR, null);
    }

    public static <T> Result<T> error(String message) {
        return error(CommonErrorCode.SYSTEM_INNER_ERROR, message);
    }

    public static <T> Result<T> error(String errorCode, String message) {
        Result<T> result = new Result<>();
        result.setCode(errorCode);
        result.setMessage(message);
        return result;
    }

    public static <T> Result<T> error(ErrorCode errorCode, String message) {
        Result<T> result = new Result<>();
        result.setCode(errorCode.getCode());
        result.setMessage(isEmpty(message) ? errorCode.getDesc() : message);
        return result;
    }


    private static boolean isEmpty(String str) {
        return str == null || str.length() < 0;
    }

    /**
     * 重写tostring 如果返回结果中包含了list，并且list的记录数超过了20，就用[...]来代替
     *
     * @return
     */
    @Override
    public String toString() {
        String resultTemp = null;
        if (getData() != null) {
            if (getData() instanceof Collection && ((Collection) getData()).size() > 20) {
                resultTemp = getData().getClass().getSimpleName() + "[...]";
            } else {
                resultTemp = this.getData().toString();
            }
        }
        //jdk1.7中已经对字符串的“+”进行了优化，用“+”拼接字符串会在执行时转为stringBuffer，效率不受影响
        return "Result(data=" + resultTemp + ",code=" + this.getCode() + ",message=" + this.getMessage() + ")";
    }
}